//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ContinuousFloatingLookbackOption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/TypePayoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/OneAssetOption.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CContinuousFloatingLookbackOption::CContinuousFloatingLookbackOption (Double currentMinmax, Cephei::QL::Instruments::ITypePayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer) : COneAssetOption(CContinuousFloatingLookbackOption::typeid)
{
    CTypePayoff^ _Cpayoff;
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phContinuousFloatingLookbackOption = NULL;
#endif
        QuantLib::Real _currentMinmax = (QuantLib::Real)ValueHelper::Convert (currentMinmax); //d
        _Cpayoff = safe_cast<CTypePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::TypePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::TypePayoff>&> (_Cpayoff->GetShared ()); 
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        _ppContinuousFloatingLookbackOption = new boost::shared_ptr<QuantLib::ContinuousFloatingLookbackOption> (new QuantLib::ContinuousFloatingLookbackOption ( _currentMinmax,  _payoff,  _exercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppContinuousFloatingLookbackOption)->setPricingEngine (_QL_Pricer);
        SetOneAssetOption (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppContinuousFloatingLookbackOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Instruments::CContinuousFloatingLookbackOption::CContinuousFloatingLookbackOption (boost::shared_ptr<QuantLib::ContinuousFloatingLookbackOption>& childNative, Object^ owner) : COneAssetOption(CContinuousFloatingLookbackOption::typeid)
{
#ifdef HANDLE
	_phContinuousFloatingLookbackOption = NULL;
#endif
	_ppContinuousFloatingLookbackOption = &childNative;
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppContinuousFloatingLookbackOption));
}
Cephei::QL::Instruments::CContinuousFloatingLookbackOption::CContinuousFloatingLookbackOption (QuantLib::ContinuousFloatingLookbackOption& childNative, Object^ owner) : COneAssetOption(CContinuousFloatingLookbackOption::typeid)
{
#ifdef HANDLE
	_phContinuousFloatingLookbackOption = NULL;
#endif
	_ppContinuousFloatingLookbackOption = new boost::shared_ptr<QuantLib::ContinuousFloatingLookbackOption> (&childNative);
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppContinuousFloatingLookbackOption));
    _ContinuousFloatingLookbackOptionOwner = owner;
    _OneAssetOptionOwner = owner;
}

Cephei::QL::Instruments::CContinuousFloatingLookbackOption::CContinuousFloatingLookbackOption (CContinuousFloatingLookbackOption^ copy) : COneAssetOption(CContinuousFloatingLookbackOption::typeid)
{
#ifdef HANDLE
	_phContinuousFloatingLookbackOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppContinuousFloatingLookbackOption = new boost::shared_ptr<QuantLib::ContinuousFloatingLookbackOption> (copy->GetShared());
        _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppContinuousFloatingLookbackOption));
    }
}
Cephei::QL::Instruments::CContinuousFloatingLookbackOption::CContinuousFloatingLookbackOption (PLATFORM::Type^ t) : COneAssetOption(CContinuousFloatingLookbackOption::typeid)
{
#ifdef HANDLE
	_phContinuousFloatingLookbackOption = NULL;
#endif
	if (!t->IsSubclassOf(CContinuousFloatingLookbackOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CContinuousFloatingLookbackOption::CContinuousFloatingLookbackOption (QuantLib::Handle<QuantLib::ContinuousFloatingLookbackOption>& childNative, Object^ owner)  : COneAssetOption(CContinuousFloatingLookbackOption::typeid)
{
	_phContinuousFloatingLookbackOption = &childNative;
	_ppContinuousFloatingLookbackOption = &static_cast<boost::shared_ptr<QuantLib::ContinuousFloatingLookbackOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppContinuousFloatingLookbackOption));
    _ContinuousFloatingLookbackOptionOwner = owner;
}
Cephei::QL::Instruments::CContinuousFloatingLookbackOption::CContinuousFloatingLookbackOption (QuantLib::Handle<QuantLib::ContinuousFloatingLookbackOption> childNative)  : COneAssetOption(CContinuousFloatingLookbackOption::typeid)
{
	_phContinuousFloatingLookbackOption = &childNative;
	_ppContinuousFloatingLookbackOption = &static_cast<boost::shared_ptr<QuantLib::ContinuousFloatingLookbackOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppContinuousFloatingLookbackOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CContinuousFloatingLookbackOption::CContinuousFloatingLookbackOption (QuantLib::ContinuousFloatingLookbackOption childNative)  : COneAssetOption(CContinuousFloatingLookbackOption::typeid)
{
#ifdef HANDLE
	_phContinuousFloatingLookbackOption = NULL;
#endif
	_ppContinuousFloatingLookbackOption = new boost::shared_ptr<QuantLib::ContinuousFloatingLookbackOption> (new QuantLib::ContinuousFloatingLookbackOption (childNative));
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppContinuousFloatingLookbackOption));
}
#endif

Cephei::QL::Instruments::CContinuousFloatingLookbackOption::~CContinuousFloatingLookbackOption ()
{
    if (_ppContinuousFloatingLookbackOption != NULL)
    {
	    delete _ppContinuousFloatingLookbackOption;
        _ppContinuousFloatingLookbackOption = NULL;
    }
}
Cephei::QL::Instruments::CContinuousFloatingLookbackOption::!CContinuousFloatingLookbackOption ()
{
    if (_ppContinuousFloatingLookbackOption != NULL)
    {
	    delete _ppContinuousFloatingLookbackOption;
    }
}
QuantLib::ContinuousFloatingLookbackOption& Cephei::QL::Instruments::CContinuousFloatingLookbackOption::GetReference ()
{
    if (_ppContinuousFloatingLookbackOption == NULL) throw REFNEW NativeNullException ();
	return **_ppContinuousFloatingLookbackOption;
}
boost::shared_ptr<QuantLib::ContinuousFloatingLookbackOption>& Cephei::QL::Instruments::CContinuousFloatingLookbackOption::GetShared ()
{
    if (_ppContinuousFloatingLookbackOption == NULL) throw REFNEW NativeNullException ();
	return *_ppContinuousFloatingLookbackOption;
}
QuantLib::ContinuousFloatingLookbackOption* Cephei::QL::Instruments::CContinuousFloatingLookbackOption::GetPointer ()
{
    if (_ppContinuousFloatingLookbackOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppContinuousFloatingLookbackOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ContinuousFloatingLookbackOption>& Cephei::QL::Instruments::CContinuousFloatingLookbackOption::GetHandle ()
{
	if (_phContinuousFloatingLookbackOption == NULL)
	{
		_phContinuousFloatingLookbackOption = new Handle<QuantLib::ContinuousFloatingLookbackOption> (*_ppContinuousFloatingLookbackOption);
	}
	return *_phContinuousFloatingLookbackOption;
}
#endif
bool Cephei::QL::Instruments::CContinuousFloatingLookbackOption::HasNative () 
{
	return (_ppContinuousFloatingLookbackOption != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IContinuousFloatingLookbackOption^ Cephei::QL::Instruments::CContinuousFloatingLookbackOption_Factory::Create (Double currentMinmax, Cephei::QL::Instruments::ITypePayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CContinuousFloatingLookbackOption ( currentMinmax,  payoff,  exercise,  QL_Pricer);
}
